//
//  ViewController.m
//  UI21-NetWork
//
//  Created by 韩旭 on 16/8/11.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageNewImage;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.imageNewImage.image = [UIImage imageNamed:@"1.jpg"];
    [self post];
   
}
- (IBAction)同步请求:(id)sender {
NSURL *url = [NSURL URLWithString:@"http://www.people.com.cn/mediafile/pic/20141211/50/9980716060794538974.jpg"];
    NSMutableURLRequest *imageUrlRequest = [NSMutableURLRequest requestWithURL:url];
    imageUrlRequest.HTTPMethod = @"GET";
    NSURLResponse *response = nil;
    
//    同步请求,
//    参数1. 请求Request
//    参数2. response 的指针
//    惨数3. error的地址
  NSData *data = [NSURLConnection sendSynchronousRequest:imageUrlRequest returningResponse:&response error:nil];
    
    if (data) {
        _imageNewImage.image = [UIImage imageWithData:data];
    }
}
- (IBAction)异步请求:(id)sender {
    NSURL *url = [NSURL URLWithString:@"http://www.people.com.cn/mediafile/pic/20141211/50/9980716060794538974.jpg"];
    NSMutableURLRequest *imageUrlRequest = [NSMutableURLRequest requestWithURL:url];
    imageUrlRequest.HTTPMethod = @"GET";
    [NSURLConnection sendAsynchronousRequest:imageUrlRequest queue:[NSOperationQueue new] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        
        _imageNewImage.image = [UIImage imageWithData:data];
    }];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"点击");
}
- (IBAction)Session异步:(id)sender {
    NSURLSession *sessoion = [NSURLSession sharedSession];
    NSURL *url = [NSURL URLWithString:@"http://mi.gdt.qq.com/gdt_mview.fcg?posw=720&count=10&posh=1280&r=0.3328680072760495&datatype=2&posid=8791303589035103439&adposcount=1&ext=%7B%22req%22%3A%7B%22sdk_src%22%3A%22LiteSDK%22%2C%22jsver%22%3A%220%22%2C%22c_dpi%22%3A320%2C%22conn%22%3A1%2C%22sdkver%22%3A%225.0%22%2C%22hostver%22%3A%226.1.1.10%22%2C%22c_w%22%3A360%2C%22c_device%22%3A%22R6007%22%2C%22muid%22%3A%22e7df59afef5008973c4d32d28ea369c3%22%2C%22c_mf%22%3A%22OPPO%22%2C%22postype%22%3A4%2C%22tmpallpt%22%3Atrue%2C%22carrier%22%3A1%2C%22c_pkgname%22%3A%22com.tencent.qqmusic%22%2C%22hosttype%22%3A%22com.tencent.qqmusic%22%2C%22c_os%22%3A%22android%22%2C%22c_h%22%3A640%2C%22c_ori%22%3A0%7D%7D"];
    NSMutableURLRequest *imageUrlRequest = [NSMutableURLRequest requestWithURL:url];
    imageUrlRequest.HTTPMethod = @"GET";
//    URLSession ， 网络请求
//    参数1. request
//    参数2. Block块, callBack回调
//    注意：session的回调中处于异步线程, 需要返回主线程刷新UI(图片赋值， tableView 和 collectionView 刷新)
   NSURLSessionDataTask *task = [sessoion dataTaskWithRequest:imageUrlRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//       主线程执行图片加载
       dispatch_async(dispatch_get_main_queue(), ^{
  NSDictionary *dci =        [NSJSONSerialization JSONObjectWithData:data options:9 error:nil];
           NSLog(@"%@", dci);
       });
  }];
//    让 dataTask 执行
    
    [task resume];

}

- (void)post {
    NSURLSession *urlSession = [NSURLSession sharedSession];
    NSURL *imageURL = [NSURL URLWithString:@"http://open3.bantangapp.com/base/app/init"];
    NSMutableURLRequest *imageURLRequest = [NSMutableURLRequest requestWithURL:imageURL];
    imageURLRequest.HTTPMethod = @"POST";
    imageURLRequest.HTTPBody = [@"app_id=com.jzyd.BanTang&app_installtime=1467276561&app_versions=5.8.5&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&device_token=1aa2741a8e58dbf700d96e588b551a5bf7f8a1e222eb9948c836ba717ce0d48e&os_versions=10.0&screensize=750&track_device_info=iPhone8%2C1&track_deviceid=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&v=17" dataUsingEncoding:NSUTF8StringEncoding];
    // URLSession, 网络请求
    // 参数1: request
    // 参数2: Block块, CallBack回调
    // 注意: Session的回调中处于异步线程, 需要返回主线程刷新UI(图片赋值, TableView CollectionView的刷新)
    NSURLSessionDataTask *dataTask = [urlSession dataTaskWithRequest:imageURLRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        // 主线程执行图片加载
        dispatch_async(dispatch_get_main_queue(), ^{
            //            _networkImage.image = [UIImage imageWithData:data];
            id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"%@", result);
        });
    }];
    // 让 dataTask 执行
    [dataTask resume];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
